/**
 * Stores items with undo/redo logic.
 */
function ActionStack() {
	this.position = -1; //Pointer marks the current state
	this.savedPosition = -1;
	this.array = new Array();
}

/**
 * Moves the pointer on step up, adds the item, and throws away 
 * all items succeeding it.
 * The pointer points at the current state.
 */
ActionStack.prototype.storeAction = function(item) {
	this.position++;
	this.array[this.position] = item;
	
	//Slice away stored redo-actions
	this.array = this.array.slice(0, this.position+1);
}

/**
 *	Moves the pointer one step down and returns that item.
 *	In doing so, the pointer points at the current state.
 */
ActionStack.prototype.undoAction = function() {
	if(this.undoAvailable()) {
		this.position--;
		return this.array[this.position];
	} else {
		return false;
	}
}

/**
 * Moves the pointer up one step and returns that item.
 * In doing so, the pointer points at the current state.
 */
ActionStack.prototype.redoAction = function() {
	if(this.redoAvailable()) {
		this.position++;
		return this.array[this.position];
	} else {
		alert('ERROR');
		return false;
	}
}

/**
 * Returns true if there are redos available.
 * Otherwise it returns false.
 */
ActionStack.prototype.redoAvailable = function() {
	return (this.position < (this.array.length-1));
}

/**
 * Returns true if there are undos available.
 * Otherwise it returns false.
 */
ActionStack.prototype.undoAvailable = function() {
	return (this.position > 0);
}

/**
 * Marks the current state as saved.
 */
ActionStack.prototype.markSaved = function() {
	this.savedPosition = this.position;
}

/**
 * Returns true if the current state is marked as saved.
 * Otherwise it returns false.
 */
ActionStack.prototype.isSaved = function() {
	return (this.position == this.savedPosition);
}